package com.victorvieux.android.vlcmote;

import com.victorvieux.android.dialogs.colorDiag;
import com.victorvieux.android.dialogs.colorDiag.OnColorChangedListener;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;

public class prefs extends PreferenceActivity {
	public static final int STATE_PREFS = 3;
	private int savedColor = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
    	prefs.this.setResult(STATE_PREFS);
    	
    	Preference customPref = (Preference) findPreference("langPref");
		customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
 
			public boolean onPreferenceClick(Preference preference) {
				Dialog d = new Dialog(prefs.this);
				d.setTitle(R.string.PrefsLang);
				d.setContentView(R.layout.lang);
				d.show();
				return true;
			}
 
		});
		
	}       	
	
	@Override
	protected void onStart() {
		super.onStart();
		final int textColor = getPrefs();
		savedColor = textColor;
		Preference customPref = (Preference) findPreference("colorPref");
		customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
 
			public boolean onPreferenceClick(Preference preference) {
				OnColorChangedListener colorL = new OnColorChangedListener() {
					public void colorChanged(int color) {
						savedColor = color;
					}
				};
				colorDiag cd = new colorDiag(prefs.this,colorL,textColor);
				cd.show();
				return true;
			}
 
		});
		
	};
	
	@Override
	protected  void onPause() {
		super.onPause();
		setPrefs();
	  }
	
    private void setPrefs() {
        SharedPreferences settings = getSharedPreferences(conf.PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        CheckBoxPreference checkboxSeek = (CheckBoxPreference) findPreference("checkboxSeek");
        editor.putBoolean("checkboxSeek", checkboxSeek.isChecked());
   
        CheckBoxPreference checkBoxNoSleep = (CheckBoxPreference) findPreference("checkBoxNoSleep");
        editor.putBoolean("checkBoxNoSleep", checkBoxNoSleep.isChecked());

        ListPreference listSizes = (ListPreference) findPreference("sizePref");
        editor.putString("sizePref", listSizes.getValue());
        editor.putInt("textColor", savedColor);
        
        editor.commit();
	}
	
    private int getPrefs() {
        SharedPreferences settings = getSharedPreferences(conf.PREFS_NAME, 0);
        CheckBoxPreference checkboxSeek = (CheckBoxPreference) findPreference("checkboxSeek");
        checkboxSeek.setDefaultValue(settings.getBoolean("checkboxSeek", false));
        
        CheckBoxPreference checkBoxNoSleep = (CheckBoxPreference) findPreference("checkBoxNoSleep");
        checkBoxNoSleep.setDefaultValue(settings.getBoolean("checkBoxNoSleep", false));
        
        ListPreference listSizes = (ListPreference) findPreference("sizePref");
        listSizes.setValue(settings.getString("sizePref", "16"));
        
        return settings.getInt("textColor", 0xff4500);
	}
}